﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Payroll;

namespace pay_mng
{
    public partial class RegisterForm : Form
    {
        private Employee employee;
        private PayrollDatabase payrollDatabase = MySqlPayrollDatabase.Instance;
        public RegisterForm(Employee employee)
        {
            this.employee = employee;
            InitializeComponent();
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<int, string> kv = this.typeComboBox.SelectedItem is KeyValuePair<int, string>
                ? (KeyValuePair<int, string>) this.typeComboBox.SelectedItem
                : new KeyValuePair<int, string>();
            // 1 打卡 2 销售条 3 服务费
            if (kv.Key == 1)
            {
                this.label2.Visible = false;
                this.priceTextBox.Visible = false;
                this.numberTextBox.Visible = true;
                this.numberLabel.Text = "小时数";
                this.numberLabel.Visible = true;
            }
            else if(kv.Key == 2)
            {
                this.label2.Visible = false;
                this.priceTextBox.Visible = false;
                this.numberTextBox.Visible = true;
                this.numberLabel.Text = "销售数";
                this.numberLabel.Visible = true;
            }
            else
            {
                this.numberLabel.Visible = false;
                this.numberTextBox.Visible = false;
                this.priceTextBox.Visible = true;
                this.label2.Visible = true;
            }
    
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            Transaction tx;
            KeyValuePair<int, string> kv = this.typeComboBox.SelectedItem is KeyValuePair<int, string>
                ? (KeyValuePair<int, string>)this.typeComboBox.SelectedItem
                : new KeyValuePair<int, string>();

            // 1 打卡 2 销售条 3 服务费
            if (kv.Key == 1)
            {
                this.priceTextBox.Enabled = false;
                this.numberTextBox.Enabled = true;
                this.numberLabel.Text = "小时数";
                if (String.IsNullOrEmpty(this.numberTextBox.Text))
                {
                    MessageBox.Show("小时数不能为空", "提示");
                    return;
                }
                // 创建打卡事务
                tx = new TimeCardTransaction(this.dateTimePicker1.Value,
                    double.Parse(this.numberTextBox.Text),
                    employee.EmpId,
                    this.payrollDatabase);
            }
            else if (kv.Key == 2)
            {
                this.priceTextBox.Enabled = false;
                this.numberTextBox.Enabled = true;
                this.numberLabel.Text = "销售数";
                if (String.IsNullOrEmpty(this.numberTextBox.Text))
                {
                    MessageBox.Show("销售数不能为空", "提示");
                    return;
                }

                // 创建提交销售单事务
                tx = new SalesReceiptTransaction(this.dateTimePicker1.Value,
                    double.Parse(this.numberTextBox.Text),
                    employee.EmpId,
                    this.payrollDatabase);
            }
            else
            {
                this.numberTextBox.Enabled = false;
                this.priceTextBox.Enabled = true;
                UnionAffiliation unionAffiliation = this.employee.Affiliation as UnionAffiliation;
                if (unionAffiliation != null)
                {
                    // 创建缴纳服务费事务
                    tx = new ServiceChargeTransaction(unionAffiliation.MemberId,
                        this.dateTimePicker1.Value,
                        double.Parse(this.priceTextBox.Text),
                        this.payrollDatabase);
                }
                else
                {
                    MessageBox.Show("该雇员不是工会成员", "错误");
                    return;
                }
            }

            try
            {
                // 执行事务
                tx.Execute();
                this.Close();
            }
            catch (ApplicationException exp)
            {
                MessageBox.Show(exp.Message, "错误");
            }
        }
    }
}
